package com.apicloud.nodulecreatenotification;

import android.util.Log;

import com.uzmap.pkg.uzcore.UZWebView;
import com.uzmap.pkg.uzcore.uzmodule.UZModule;
import com.uzmap.pkg.uzcore.uzmodule.UZModuleContext;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class Dome extends UZModule {

    public Dome(UZWebView webView) {
        super(webView);
        String name = getFeatureValue("createNotification", "name");
        if(name!=null) CreateNotification.channelName = name;
        String description = getFeatureValue("createNotification", "description");
        if(description!=null) CreateNotification.channelDescription = description;
    }

    public void jsmethod_test(UZModuleContext moduleContext) throws JSONException {
        OkHttpClient client = new OkHttpClient();
        Request req = new Request.Builder().url("http://s94.top/api/test/test").addHeader("Cookie","name=123").build();
        client.newCall(req).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("httpError", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.code()==200) {
                    Log.e("httpSuccess", response.body().string());
                }else{
                    Log.e("httpFail", response.body().string());
                }
            }
        });
    }
    public void jsmethod_create(UZModuleContext moduleContext) throws JSONException {
        JSONObject ret = new JSONObject();
        CreateNotification createNotification = new CreateNotification(context(),moduleContext);
        createNotification.title = moduleContext.optString("title","");
        createNotification.content = moduleContext.optString("content","");
        createNotification.autoCancel = moduleContext.optBoolean("autoCancel",true);
        createNotification.ongoing = moduleContext.optBoolean("ongoing",false);
        createNotification.img = moduleContext.optString("img",null);
        createNotification.data = moduleContext.optJSONObject("data");
        int id = createNotification.create();

        ret.put("status",true).put("msg","操作成功").put("id",id);
        moduleContext.success(ret,false);
    }

    public void jsmethod_update(UZModuleContext moduleContext) throws JSONException {
        JSONObject ret = new JSONObject();
        int id = moduleContext.optInt("id",0);
        if(!CreateNotification.list.containsKey(id+"")){
            ret.put("status", false).put("msg", "通知消息不存在");
            moduleContext.success(ret,true);
            return;
        }
        CreateNotification createNotification = CreateNotification.list.get(id+"");
        String title = moduleContext.optString("title",null);
        String content = moduleContext.optString("content",null);
        String img = moduleContext.optString("img",null);
        Double progress = moduleContext.optDouble("progress",-1);
        if (title!=null) createNotification.title = title;
        if (content!=null) createNotification.content = content;
        if (img!=null) createNotification.img = img;
        createNotification.progress = (int)Math.round(progress*1000);

        createNotification.create();
        ret.put("status", true).put("msg", "操作成功");
        moduleContext.success(ret,true);
    }

    public void jsmethod_close(UZModuleContext moduleContext) throws JSONException {
        JSONObject ret = new JSONObject();
        int id = moduleContext.optInt("id",0);
        Log.e("err", id+"");
        if(!CreateNotification.list.containsKey(id+"")){
            ret.put("status", false).put("msg", "通知消息不存在");
            moduleContext.success(ret,true);
            return;
        }
        CreateNotification.list.get(id+"").cancel();
        ret.put("status", true).put("msg", "操作成功");
    }



}
